package com.gxa.saas.service.impl;

import com.gxa.saas.mapper.RouteDetailMapper;
import com.gxa.saas.pojo.RouteDetail;
import com.gxa.saas.service.RouteDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


@Service
@Transactional(rollbackFor = Throwable.class)
public class RouteDetailServiceImpl implements RouteDetailService {

    @Autowired
    RouteDetailMapper routeDetailMapper;

    @Override
    public void up(Integer id,String name){
        List<RouteDetail> routeDetails = routeDetailMapper.selectByName(name);
        List<Integer>  idList=new ArrayList<>();
        for (int i = 0; i < routeDetails.size(); i++) {
            idList.add(routeDetails.get(i).getId());
        }
        Integer index=null;
        for (int j = 0; j < idList.size(); j++) {
            if(idList.get(j).equals(id) && j!=0){
                index=j;
            }
        }

if(index!=null){
    Integer id2=idList.get(index-1);

    routeDetailMapper.updateId(20000,id);
    routeDetailMapper.updateId(id,id2);
    routeDetailMapper.updateId(id2,20000);
}


    }

    @Override
    public void down(Integer id,String name) {

        List<RouteDetail> routeDetails = routeDetailMapper.selectByName(name);
        List<Integer>  idList=new ArrayList<>();
        for (int i = 0; i < routeDetails.size(); i++) {
            idList.add(routeDetails.get(i).getId());
        }
        Integer index=null;
        for (int j = 0; j < idList.size(); j++) {
            if(idList.get(j).equals(id) && j!=(idList.size()-1)){
                index=j;
            }
        }


if(index!=null){
    Integer id2=idList.get(index+1);

    routeDetailMapper.updateId(50000,id);
    routeDetailMapper.updateId(id,id2);
    routeDetailMapper.updateId(id2,50000);
}


    }

    @Override
    public void first(Integer id) {
        List<RouteDetail> routeDetails = routeDetailMapper.selectAll();
        Integer id2= routeDetails.get(0).getId();
        Integer id1=id2-1;

        //判断是否是第一位
        if(id.equals(id2)){
           return;
        }

        routeDetailMapper.updateId(id1,id);
    }

    @Override
    public void last(Integer id) {

        List<RouteDetail> routeDetails = routeDetailMapper.selectAll();
        Integer id2 = routeDetails.get(routeDetails.size() - 1).getId();
        Integer id1=id2+1;

        if(id.equals(id2)){
            return;
        }
        routeDetailMapper.updateId(id1,id);
    }

    @Override
    public void delete(Integer id) {
        RouteDetail routeDetail = routeDetailMapper.selectById(id);
        if(routeDetail==null || routeDetail.equals("")){
            return;
        }
        routeDetailMapper.delete(id);
    }

    @Override
    public void add(RouteDetail routeDetail) {
        routeDetailMapper.add(routeDetail);
    }

    @Override
    public void deleteAll(String name) {
        routeDetailMapper.deleteAll(name);
    }


    @Override
    public List<RouteDetail> selectByName(String name) {
        return routeDetailMapper.selectByName(name);
    }


}
